﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GenericGameLibrary;
using OpenTK.Graphics.OpenGL;

namespace SimpleTriangle
{
    public partial class OpenTK_DEMO : Form
    {
        FastLoop FunctLoop;

        public OpenTK_DEMO()
        {
            InitializeComponent();
        }

        private void OpenTK_DEMO_Load(object sender, EventArgs e)
        {
            glcDisplay.Dock = DockStyle.Fill;
            glcDisplay.MakeCurrent();
            GL.Viewport(0, 0, this.ClientSize.Width, this.ClientSize.Height);

            GL.Enable(EnableCap.DepthTest); // Está desactivado por defecto en OpenGL
            GL.Enable(EnableCap.CullFace); // Está desactivado por defecto en OpenGL
       
            GL.Enable(EnableCap.ColorMaterial);

            FunctLoop = new FastLoop(screenRefresh);
            screenInit(20,0,0,100);
        }

        void screenInit(float red, float green, float blue, float alpha)
        {
            GL.ClearColor(red / 100, green / 100, blue / 100, alpha / 100);
            
        }

        void screenRefresh(double elapsedTime)
        {
            
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            GL.Rotate(elapsedTime * 100, .5, 1, .5);
            BuildTriangle(); //Builds a triangle
            
            GL.Finish();

            glcDisplay.SwapBuffers();
            glcDisplay.Refresh();
        }

        void BuildTriangle()
        {
            GL.Begin(BeginMode.Quads);
            {
                GL.Color3(Color.Red);
                GL.Vertex3(-.1f, -.10f, -.10f);
                GL.Vertex3(-0.1f, 0.1f, -0.1f);
                GL.Vertex3(0.1f, 0.1f, -0.1f);
                GL.Vertex3(0.1f, -0.1f, -0.1f);

                GL.Color3(Color.Green);
                GL.Vertex3(-0.1f, -0.1f, -0.1f);
                GL.Vertex3(0.1f, -0.1f, -0.1f);
                GL.Vertex3(0.1f, -0.1f, 0.1f);
                GL.Vertex3(-0.1f, -0.1f, 0.1f);

                GL.Color3(Color.Blue);

                GL.Vertex3(-0.1f, -0.1f, -0.1f);
                GL.Vertex3(-0.1f, -0.1f, 0.1f);
                GL.Vertex3(-0.1f, 0.1f, 0.1f);
                GL.Vertex3(-0.1f, 0.1f, -0.1f);

                GL.Color3(Color.Red);
                GL.Vertex3(-0.1f, -0.1f, 0.1f);
                GL.Vertex3(0.1f, -0.1f, 0.1f);
                GL.Vertex3(0.1f, 0.1f, 0.1f);
                GL.Vertex3(-0.1f, 0.1f, 0.1f);

                GL.Color3(Color.Green);
                GL.Vertex3(-0.1f, 0.1f, -0.1f);
                GL.Vertex3(-0.1f, 0.1f, 0.1f);
                GL.Vertex3(0.1f, 0.1f, 0.1f);
                GL.Vertex3(0.1f, 0.1f, -0.1f);

                GL.Color3(Color.Blue);
                GL.Vertex3(0.1f, -0.1f, -0.1f);
                GL.Vertex3(0.1f, 0.1f, -0.1f);
                GL.Vertex3(0.1f, 0.1f, 0.1f);
                GL.Vertex3(0.1f, -0.1f, 0.1f);
                
            }
            GL.End();

        }
    }
}
